<!--
    Author : Gisca
    Controller dari Mengelola Role Pengguna
-->

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

include 'page/abstractPage.php';

class ServerMengelolaRolePengguna extends AbstractPage {

    private $action='';
    private $message='';
    private $tabelPengguna='';
    private $tabelDataPengguna='';
    private $userid='';
    private $checkAdmin='';
    private $checkDosen='';
    private $checkTU='';
    private $checkMahasiswa='';
    private $checkDekan='';

    public function ServerMengelolaRolePengguna()
    {
        parent::__construct();
        $this->load->model('Pengguna');
        $this->load->model('Role');

        //membuat template tabel
        $tmpl = array (
                    'table_open'          => '<table border="1" cellpadding="4" cellspacing="0" class="table1">',

                    'heading_row_start'   => '<tr>',
                    'heading_row_end'     => '</tr>',
                    'heading_cell_start'  => '<th>',
                    'heading_cell_end'    => '</th>',

                    'row_start'           => '<tr>',
                    'row_end'             => '</tr>',
                    'cell_start'          => '<td>',
                    'cell_end'            => '</td>',

                    'row_alt_start'       => '<tr>',
                    'row_alt_end'         => '</tr>',
                    'cell_alt_start'      => '<td>',
                    'cell_alt_end'        => '</td>',

                    'table_close'         => '</table>'
              );
        $this->table->set_template($tmpl);
    }

    public function DaftarPengguna()
    {
        $this->action='';
     
        //Build data pengguna
        $hasil=$this->Pengguna->getAllPengguna();

        $this->table->set_heading('Username','Fullname','Aksi');
        
        foreach ($hasil as $row)
        {
            $this->table->add_row($row->userID,$row->name, anchor('ServerMengelolaRolePengguna/editRolePengguna/'.$row->userID,'Edit Role'));
        }

        $this->tabelPengguna=$this->table->generate();

        $this->index();
    }

    public function editRolePengguna($user_id)
    {
        $this->load->model('Role','role');
        $this->action="editRolePengguna";
        $this->userid = $user_id;
        //query
        $hasil=$this->Role->getRolePengguna($user_id);
        foreach ($hasil as $row)
        {
            if($row['roleName']=="Admin"){
                $this->checkAdmin = $row['roleName'];
            }
            else if($row['roleName']=="TU"){
                $this->checkTU = $row['roleName'];
            }
            else if($row['roleName']=="Dosen"){
                $this->checkDosen = $row['roleName'];
            }
            else if($row['roleName']=="Mahasiswa"){
                $this->checkMahasiswa = $row['roleName'];
            }
            else if($row['roleName']=="Dekan"){
                $this->checkDekan = $row['roleName'];
            }
        }

        $this->index();
    }

    public function simpanEditRolePengguna()
    {
        $this->load->model('Role','role');
        $daftarRole = $_POST['daftarRole'];
        //$daftarNotRole = $_POST['daftarNotRole'];
        $pat = ',';
        $roleawal=explode($pat,$daftarRole);
        //$notrole=split($pat,$daftarNotRole);
        $this->role->deleteRole($_POST['user_id']);
        $i = 0;        
        while($roleawal[$i]!=null){
            $this->role->updateRolePengguna($roleawal[$i],$_POST['user_id']);
            $i++;
        }
        $this->message = 'Berhasil update role';
        $this->DaftarPengguna();
    }
        
    function _buildContent()
    {
        if ($this->action=='')
        {
            $data = array (
                'tabelPengguna' => $this->tabelPengguna,
                'message' => $this->message
            );
            $this->load->view('MengelolaRolePengguna/DaftarPengguna',$data);

        }
        else if ($this->action=='editRolePengguna')
        {
             $data = array (
                'userid' => $this->userid,
                'checkAdmin' => $this->checkAdmin,
                'checkTU' => $this->checkTU,
                'checkDosen' => $this->checkDosen,
                'checkMahasiswa' => $this->checkMahasiswa,
                'checkDekan' => $this->checkDekan
            );
            $this->load->view('MengelolaRolePengguna/editRolePengguna',$data);
        }
        
    } 
}
